import {Injectable} from '@angular/core';
import {ApplicationRecord} from '../types/resp/ApplicationRecord';
import {ApplicationRecordForm} from '../types/req/ApplicationRecordForm';
import {Result} from '../types/result';
import MockApi from '../api/mockApi';
import {map, Observable} from 'rxjs';
import type {Response} from '../types/response';
import {Page} from '../types/page';
import {ApplicationRecordQueryForm} from '../types/req/ApplicationRecordQueryForm';

@Injectable({
  providedIn: 'root'
})
export class ApplicationRecordService {
  applicationRecords: ApplicationRecord[] = [
    {"id":1,"enterpriseId":2,"enterpriseName":"腾讯科技","jobDescriptionId":1,"jobDescriptionName":"后端开发工程师","candidateId":1,"candidateName":"王强","status":90,"createTime":"2024-08-24 22:43:13","modifyTime":"2024-01-18 22:43:13"},
    {"id":2,"enterpriseId":3,"enterpriseName":"阿里健康","jobDescriptionId":2,"jobDescriptionName":"数据分析师","candidateId":3,"candidateName":"张伟","status":10,"createTime":"2024-06-02 22:43:13","modifyTime":"2024-11-12 22:43:13"},
    {"id":3,"enterpriseId":4,"enterpriseName":"新东方教育","jobDescriptionId":2,"jobDescriptionName":"数据分析师","candidateId":2,"candidateName":"陈静","status":30,"createTime":"2024-04-01 22:43:13","modifyTime":"2024-02-07 22:43:13"},
    {"id":4,"enterpriseId":3,"enterpriseName":"阿里健康","jobDescriptionId":2,"jobDescriptionName":"数据分析师","candidateId":3,"candidateName":"张伟","status":90,"createTime":"2024-02-29 22:43:13","modifyTime":"2024-09-07 22:43:13"},
    {"id":5,"enterpriseId":2,"enterpriseName":"腾讯科技","jobDescriptionId":4,"jobDescriptionName":"产品经理","candidateId":2,"candidateName":"陈静","status":20,"createTime":"2025-01-01 22:43:13","modifyTime":"2024-07-30 22:43:13"},
    {"id":6,"enterpriseId":4,"enterpriseName":"新东方教育","jobDescriptionId":1,"jobDescriptionName":"后端开发工程师","candidateId":1,"candidateName":"王强","status":30,"createTime":"2024-01-16 22:43:13","modifyTime":"2024-09-01 22:43:13"}
  ]
  constructor() {
  }

  save (applicationRecordForm: ApplicationRecordForm): Observable<Result<void>> {
    console.log('applicationRecordApi save params', applicationRecordForm)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }

  find (applicationRecordQueryForm: ApplicationRecordQueryForm): Observable<Result<Page<ApplicationRecord>>> {
    console.log('applicationRecordApi find params', applicationRecordQueryForm)
    let filteredApplicationRecords = this.applicationRecords
    if (applicationRecordQueryForm.candidateId) {
      filteredApplicationRecords = this.applicationRecords.filter(applicationRecord => applicationRecord.candidateId === applicationRecordQueryForm.candidateId);
    }
    return MockApi.queryPageSuccessfully(filteredApplicationRecords).pipe(
      map((response: Response<Result<Page<ApplicationRecord>>>) => response.data)
    );
  }

  findById (id: number): Observable<Result<ApplicationRecord>> {
    console.log('applicationRecordApi findById id', id)
    let target = {}
    for (const a of this.applicationRecords) {
      if (a.id === id) {
        target = a
      }
    }
    return MockApi.operateSuccessfullyWithData(target).pipe(
      map((response: Response<Result<ApplicationRecord>>) => response.data)
    );
  }

  del (id: number): Observable<Result<void>> {
    console.log('applicationRecordApi del id', id)
    return MockApi.operateSuccessfully().pipe(
      map((response: Response<Result<void>>) => response.data)
    );
  }
}
